<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字段列表</title>
    <link rel="stylesheet" type="text/css" href="/static/hplus/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="/static/hplus/css/style.css">
    <link rel="stylesheet" type="text/css" href="/static/layer/skin/layer.css">
    <style type="text/css" rel="stylesheet">
        .form-control {width: 220px;}
        .ttd {width: 250px;text-align: center;}
        .ttb {width: 80px;text-align: center;}
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <form action="/customize/lists" method="get">
                        <table style="width: 100%;padding: 0;border: none">
                            <tr>
                                <td class="ttd">
                                    <select class="form-control" name="table_name">
                                        <option value="">…数据表…</option>
                                        {foreach $tables as $t}
                                        <option value="{$t.TABLE_NAME}">{$t.TABLE_COMMENT}</option>
                                        {/foreach}
                                    </select>
                                </td>
                                <td class="ttb"><input type="submit" class="btn btn-sm btn-primary" value="搜索"></td>
                                <td>
                                    <a href="javascript:;" id="add" class="btn btn-sm btn-success">添加</a>
                                    <a href="javascript:;" class="btn btn-sm btn-info" data-action="delAll">批量删除</a>
                                </td>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    </form>
                </div>
                <div class="ibox-content">
                    <table id="treeTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="/static/hplus/js/jquery.min.js?v={php}echo time(){/php}"></script>
<script type="text/javascript" src="/static/layer/layer.js?v={php}echo time(){/php}"></script>
<script type="text/javascript" src="/static/royui/royui.min.js?v={php}echo time(){/php}"></script>
<script type="text/javascript">
    $(function () {
        //let $data = JSON.parse('<?php echo json_encode($list); ?>')
        let $r = new royui()
        $r.treeTable({
            elem: '#treeTable',
            //data: $data,
            url: '/customize/lists',
            paging: true,
            first_page: true,
            prev_page: true,
            next_page: true,
            count: {$count},
            pages: {$pages},
            cols: [
                {
                    field: 'table_name',
                    title: '所属表'
                },
                {
                    field: 'sort',
                    title: '排序'
                },
                {
                    field: 'field_name',
                    title: '字段名'
                },
                {
                    field: 'field_txt',
                    title: '显示名'
                },
                {
                    field: 'field_type',
                    title: '字段类型'
                },
                {
                    field: 'ctime',
                    title: '添加时间'
                },
                {
                    field: 'mtime',
                    title: '修改时间'
                },
                {
                    title: '操作',
                    action: [{
                        matter: 'edit',
                        url: '/customize/edit',
                        area: ['700px', '750px'],
                        method: 'popup'
                    }, {matter: 'del', url: '/customize/del'}]
                }
            ]
        })
        // 添加自定义字段
        $('#add').click(function () {
            layer.open({
                type: 2,
                title: '添加自定义字段',
                content: '/customize/add',
                area: ['700px', '750px'],
                closeBtn: 2,
                btn: ['确定', '取消'],
                yes: function ($index, $layero) {
                    let $body = $layero.find('iframe').contents().find('body');
                    let $param = decodeURIComponent($($body).find('form').serialize());
                    $param = $param.split('&');
                    let $params = {};
                    $($param).each(function ($n, $obj) {
                        let $key = $obj.split('=')[0];
                        if ($key.indexOf('[]') === -1) {
                            $params[$key] = $obj.split('=')[1];
                        } else {
                            $key = $key.substring(0, $key.indexOf('[]'));
                            let $count = 0;
                            if ($params[$key] !== undefined) {
                                $count = $params[$key].length;
                            } else {
                                $params[$key] = [];
                            }
                            $params[$key][$count] = $obj.split('=')[1];
                        }
                    });
                    if (!$params.table_name) {
                        layer.msg('所属表不能为空', {icon: 5, time: 1000});
                        return false;
                    }
                    if (!$params.field_txt) {
                        layer.msg('字段名称不能为空', {icon: 5, time: 1000});
                        return false;
                    }
                    if (!$params.field_type) {
                        layer.msg('字段类型不能为空', {icon: 5, time: 1000});
                        return false;
                    }
                    if (($params.field_type === 'radio' || $params.field_type === 'select') && (!$params.fv || !$params.fk)) {
                        layer.msg('radio单选或下拉框可选值不能为空', {icon: 5, time: 1000});
                        return false;
                    }
                    $.ajax({
                        type: 'post', dataType: 'json', cache: false,
                        url: '{:url("add")}',
                        data: $params,
                        success: function ($data) {
                            if ($data.code === 200) {
                                layer.msg($data.message, {icon: 1, time: 1200}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg($data.message, {icon: 5});
                            }
                        }
                    })
                }
            })
        })
    });
</script>
</html>
